<template>
	<view class="container" :style="[containHeight]">
		<view class="header">
			<view class="layout-start title">竞赛成绩<image src="../../static/medal.png" class="medal"></image>
			</view>
			<!-- <view class="competition">
				<u-button text="去竞赛拿积分" color="linear-gradient(to right, #7774ff, #017ffd,#01b1fd)"
					@click="toCustomization"></u-button>
			</view> -->
		</view>
		<view class="content">
			<view class="search-content">
				<view class="search-item">
					<text class="search-label">日期：</text>
					<view class="dateBorder">
						<uni-datetime-picker v-model="rangeDate" type="datetimerange" :border="false" />
					</view>
				</view>
				<view class="search-item">
					<text class="search-label">部门：</text>
					<u-picker :show="showisDeptName" :columns="options.deptName" v-model="searchParams.deptName"
						@confirm="isDeptNameConfirm" @cancel="showisDeptName=false"></u-picker>
					<view class="select-border" @click="showisDeptName=true">
						<text v-if="!searchParams.deptName">请选择部门</text>
						<text v-if="searchParams.deptName" class="selectedTxt">{{searchParams.deptName}}</text>
					</view>
				</view>

				<view class="btn-group">
					<u-button text="查询" type="primary" :loading="btnSearch" loadingText="查询中" class="search-btn"
						@click="toSearch"></u-button>
				</view>
			</view>
			<view v-if="challengeList.length==0">
				<u-empty mode="list" icon="../../static/listEmpty.png"></u-empty>
			</view>
			<view class="historicalAchievements_item" v-for="(item,index) in challengeList" :key="index"
				:style="{'background-color': getBackgroundColor(index)}" v-else>
				<view class="testPaperTitle">{{item.name}}</view>
				<view class="testTime">竞赛时间：{{item.startTime}} </view>
				<view class="line"></view>
				<view class="historical_content">
					<u-row class="historical_item">
						<u-col span="3">
							<view class="historical_title"> 成绩</view>
							<view class="historical_txt bule">{{item.score}}</view>
						</u-col>
						<u-col span="3">
							<view class="historical_title">考试情况</view>
							<view class="historical_txt" v-if="item.examResults==0">未参考</view>
							<view class="historical_txt" v-if="item.examResults==1">及格</view>
							<view class="historical_txt warnTxt" v-if="item.examResults==2">不及格</view>

						</u-col>
						<u-col span="4">
							<view class="historical_title">竞赛用时</view>
							<view class="historical_txt">{{item.unavailable}}</view>
						</u-col>
						<u-col span="2">
							<view class="historical_title">总分</view>
							<view class="historical_txt">100</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<u-loadmore :status="statusLoading" v-if="challengeList.length!=0" />
		</view>
	</view>
</template>

<script>
	import {
		challengeResult,
		selectDeptName,
		challengeResultSelect
	} from "../../utils/api.js"
	export default {
		data() {
			return {
				fullHeight: true,
				challengeList: [],
				total: null, //总条数
				page: 1, //当前页码
				pageSize: 5, //每页显示数量
				userId: null,
				backgroundColors: ['#FBF2DD', '#FCFAA7', '#E3F4FA'],
				rangeDate: [],
				showisDeptName: false,
				options: {
					deptName: [], //部门					
				},
				searchParams: {
					deptName: '',
				},
				btnSearch: false,
				//仅用于像后端传递数据
				paramsValue: {
					userId: '',
					startTime: null,
					endTime: '',
					deptName: '',
					pageSize: 5,
					page: 1,
				},
				statusLoading: 'loadmore',
				nextQuery: false,
				onlyReachBottom: true,
			}
		},
		onLoad() {
			this.userId = uni.getStorageSync('userinfo').id
			this.getChallengeList()
			this.getOptions()
		},		
		onReachBottom() {
			if (this.fullHeight) {
				this.fullHeight = false
			}
			if (this.nextQuery) {
				//搜索结果
				++this.paramsValue.page
				this.btnSearch = false
				//处理参数向后端查询数据
				this.paramsValue.deptName = this.searchParams.deptName
				this.paramsValue.startTime = this.rangeDate[0]
				this.paramsValue.endTime = this.rangeDate[1]
				//过滤空的参数
				const params = Object.keys(this.paramsValue).filter((key) => this.paramsValue[key] !== null && this
					.paramsValue[key] !== '' && this.paramsValue[key] !== undefined).reduce((acc, key) => ({
					...acc,
					[key]: this.paramsValue[key]
				}), {})
				// console.log("const params", params);
				challengeResultSelect(
					params
				).then(s => {
					// console.log("查询s", s);
					if (s.code === 20000) {
						this.challengeList = this.challengeList.concat(s.data.data)
						// console.log("this.challengeList onreachbooon 查询", this.challengeList);
						this.onlyReachBottom = false
						//所有数据加载完
						if (s.data.total == this.challengeList.length) {
							this.statusLoading = 'nomore'
							this.nextQuery = false
							// console.log("s.data.total==this.challengeList.length");
						}
					} else {
						uni.showToast({
							title: s.message,
							icon: 'none'
						})
					}
				})
			} else if (this.onlyReachBottom) {
				// console.log("this.page * this.pageSize < this.total", this.page * this.pageSize < this.total);
				// console.log("1", this.page, this.pageSize, this.total);
				if (this.page * this.pageSize < this.total) {
					this.statusLoading = 'loading'
					this.page++
					this.getChallengeList()
					// console.log("onReachBottom");
				} else {
					this.statusLoading = 'nomore'

				}
			} else {
				console.log("什么也不做");
			}
		},
		computed: {
			containHeight() {
				return this.fullHeight ? {
					/*#ifdef MP-WEIXIN*/
					height: '100vh',
					/*#endif*/
					/*#ifndef MP-WEIXIN*/
					height: 'calc(100vh - 44px)'
					/*#endif*/
				} : {
					height: '100%'
				}
			},
		},		
		// onShow() {
		// 	this.fullHeight = true
		// },
		methods: {
			// toCustomization() {
			// 	uni.showModal({
			// 		title: '提示',
			// 		content: '即将跳转到题库页面，请手动选择您要参加的竞赛',
			// 		success: function(res) {
			// 			if (res.confirm) {
			// 				console.log('用户点击确定');
			// 				uni.switchTab({
			// 					url:`/pages/onlineTesting/multipleChoice`
			// 				})
			// 			} else if (res.cancel) {
			// 				console.log('用户点击取消');
			// 			}
			// 		}
			// 	});
			// },
			getChallengeList() {
				challengeResult({
					userId: this.userId,
					pageSize: this.pageSize,
					page: this.page,
				}).then(s => {
					if (s.code === 20000) {
						this.challengeList = this.challengeList.concat(s.data.data)
						this.total = s.total
					} else {
						uni.showToast({
							title: s.message,
							icon: "none"
						})
					}
				})
			},
			getBackgroundColor(index) {
				if (index >= this.backgroundColors.length) {
					index %= this.backgroundColors.length
				}
				return this.backgroundColors[index]
			},
			// 获取下拉列表
			getOptions() {
				selectDeptName().then(s => {
					if (s.code === 20000) {
						this.options.deptName = [s.data]
					} else {
						uni.showToast({
							title: s.message,
							icon: "none"
						})
					}
				})
			},
			toSearch() {
				this.paramsValue.page = 1
				this.btnSearch = true
				this.onlyReachBottom = false
				//处理参数向后端查询数据							
				this.paramsValue.deptName = this.searchParams.deptName
				this.paramsValue.startTime = this.rangeDate[0]
				this.paramsValue.endTime = this.rangeDate[1]


				//过滤空的参数
				const params = Object.keys(this.paramsValue).filter((key) => this.paramsValue[key] !== null && this
					.paramsValue[key] !== '' && this.paramsValue[key] !== undefined).reduce((acc, key) => ({
					...acc,
					[key]: this.paramsValue[key]
				}), {})
				console.log("const params", params);
				this.challengeList = []
				challengeResultSelect(
					params
				).then(s => {
					console.log("查询s", s);
					if (s.code === 20000) {
						//搜索结果反映到视图
						this.btnSearch = false
						this.challengeList = s.data.data
						this.nextQuery = true
						// this.searchList = this.searchList.concat(s.data.data)
						//查询结果的页数,向上取整
						let searchPageNum = Math.ceil(s.data.total / this.pageSize)
						this.page = 1
						if (searchPageNum > this.page) {
							//调查询接口，页码加1							
							this.statusLoading = 'loading'
							console.log("还有下一页");
						} else {
							this.statusLoading = 'nomore'
							console.log("只有当前页");
						}

					} else {
						uni.showToast({
							title: s.message,
							icon: 'none'
						})
					}
				})
			},
			isDeptNameConfirm(val) {
				this.showisDeptName = false
				this.searchParams.deptName = val.value[0]
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		// background: url("../../static/bg.png");
		// background-size: cover;	
		background: linear-gradient(#5191FF 5%, #6AA2FF 8%, #BDD7FE 15%, #F6FAFF 100%);
		padding: 30rpx 30rpx;
		width: 100%;
		position: relative;
		box-sizing: border-box;

		.header {
			// background-image: url('../../static/1 (2).png');
			// background-size: 100% 100%;
			// margin-top: 30rpx;
			// padding: 15rpx 25rpx;
			box-sizing: border-box;
			border-radius: 15rpx;
			// opacity: 0.9;
			width: 100%;
			position: relative;

			.title {
				font-weight: bold;
				padding: 35rpx 15rpx;
				box-sizing: border-box;
				font-size: 35rpx;
				color: #ffffff;
			}

			.competition {
				display: flex;
				justify-content: flex-end;
				align-items: center;
				width: 100%;
				position: relative;
				margin-top: 40rpx;

				.u-button {
					width: 30%;
				}

				uni-button {
					margin-right: unset;
				}
			}
		}

		.content {
			background: #ffffff;
			// margin-top: 30rpx;
			padding: 30rpx 32rpx 2rpx;
			box-sizing: border-box;
			border-radius: 15rpx;
			opacity: 0.9;
			width: 100%;
			position: relative;
		}
	}

	.medal {
		width: 50rpx;
		height: 50rpx;
		margin-left: 20rpx;
	}

	.historicalAchievements_item {
		background: #ffffff;
		box-sizing: border-box;
		padding: 30rpx;
		margin: 40rpx 0rpx;
		border-radius: 20rpx;
		box-shadow: 4px 4px 10px #888888;

		.testPaperTitle {
			font-size: 28rpx;
			color: #000000;
			font-weight: bold;
		}

		.testTime {
			font-size: 28rpx;
			color: #737375;
			margin-top: 10rpx;
		}

		.line {
			height: 1rpx;
			background: #737375;
			width: 100%;
			margin: 30rpx 0rpx;
		}

		.historical_content {
			.historical_item {
				width: 100%;
				display: flex;
				justify-content: space-between;

				.historical_title {
					font-size: 30rpx;
					color: #737375;
				}

				.historical_txt {
					margin-top: 10rpx;
					font-size: 30rpx;
					color: #0B0B0B;
				}

				.bule {
					color: #3888FF;
					font-size: 32rpx;
				}
			}
		}
	}

	.warnTxt {
		color: #E13124 !important;
		font-size: 30rpx !important;
	}

	.search-content {
		box-sizing: border-box;
		width: 100%;
		position: relative;
		background: #ffffff;
		padding: 20rpx 15rpx;

		.search-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			width: 100%;
			margin-top: 10rpx;
			font-size: 30rpx;
			box-sizing: border-box;
			padding: 10rpx 20rpx;

			.select-border {
				width: 80%;
				height: 38px;
				line-height: 38px;
				border: 1px solid #dadbde;
				border-radius: 50rpx;
				color: #B1B1B1;
				box-sizing: border-box;
				padding: 0rpx 25rpx;
				font-size: 16px;
				// z-index: 0;

				.selectedTxt {
					color: rgb(48, 49, 51);
				}
			}

			.select-border-has {
				width: 80%;
				height: 38px;
				line-height: 38px;
				border: 1px solid #dadbde;
				border-radius: 50rpx;
				// color: #B1B1B1;
				box-sizing: border-box;
				padding: 0rpx 25rpx;
				font-size: 16px;
				// z-index: 9;
				position: relative;
				display: flex;
				align-items: center;

				.selectedTxt {
					color: rgb(48, 49, 51);
				}
			}
		}

		.search-label {
			max-width: 300rpx;
			overflow: hidden;
			// text-overflow: ellipsis;
			white-space: nowrap;
			font-size: 30rpx;
		}

		.btn-group {
			display: flex;
			margin-top: 60rpx;
			width: 100%;
			position: relative;
			justify-content: space-between;

			.search-btn {
				width: 40%;
				border-radius: 100rpx;
				background: linear-gradient(to right, #7774ff, #017ffd, #01b1fd);
				color: #fff;
			}

			.reset-btn {
				width: 40%;
				border-radius: 100rpx;
			}
		}
	}

	.dateBorder {
		width: 80%;
		height: 38px;
		border: 1px solid #dadbde;
		border-radius: 50rpx;
		box-sizing: border-box;
		padding: 0rpx 25rpx;
		font-size: 16px;
		z-index: 1;
	}
</style>